package net.entelijan.cobean.bind.impl;

import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import net.entelijan.cobean.bind.IModelChangeListener;
import net.entelijan.cobean.bind.impl.DefaultModelWrapper;

public class DefaultModelWrapperTestCase {

    @Test
    public void testABean() {
        SimpleABean b = new SimpleABean("HELLO");
        DefaultModelWrapper w = new DefaultModelWrapper();
        ReportingModelChangeListener mcl = new ReportingModelChangeListener();
        b = w.wrapModel(b, createList(mcl));
        Assert.assertEquals("I,", mcl.getBuffer().toString());
        b.setStrVal("WORLD");
        Assert.assertEquals("I,PC,GPN,", mcl.getBuffer().toString());
        b.getStrVal();
        Assert.assertEquals("I,PC,GPN,", mcl.getBuffer().toString());
        b.setStrVal("GO HOME");
        Assert.assertEquals("I,PC,GPN,PC,GPN,", mcl.getBuffer().toString());
    }

    @Test
    public void testWrappedABean() {
        SimpleABean b = new SimpleABean("HELLO");
        DefaultModelWrapper w = new DefaultModelWrapper();
        ReportingModelChangeListener mcl = new ReportingModelChangeListener();
        b = w.wrapModel(b, createList(mcl));
        Assert.assertEquals("I,", mcl.getBuffer().toString());
        BeanWrapper bw = new BeanWrapperImpl(b);
        bw.getPropertyValue("strVal");
        Assert.assertEquals("I,", mcl.getBuffer().toString());
        bw.setPropertyValue("strVal", "WORLD");
        Assert.assertEquals("I,PC,GPN,", mcl.getBuffer().toString());
    }

    @Test
    public void testABean01() {
        SimpleABean b = new SimpleABean("HELLO");
        DefaultModelWrapper w = new DefaultModelWrapper();
        ReportingModelChangeListener mcl = new ReportingModelChangeListener();
        b = w.wrapModel(b, createList(mcl));
        Assert.assertEquals("I,", mcl.getBuffer().toString());
        b.setStrVal("WORLD");
        b.setStrVal("WORLD");
        b.setStrVal("WORLD");
        Assert.assertEquals("I,PC,GPN,PC,PC,", mcl.getBuffer().toString());
        b.getStrVal();
        Assert.assertEquals("I,PC,GPN,PC,PC,", mcl.getBuffer().toString());
        b.setStrVal("GO HOME");
        b.setStrVal("GO HOME");
        b.setStrVal("GO HOME");
        b.setStrVal("GO HOME");
        b.setStrVal("GO HOME");
        b.setStrVal("GO HOME");
        b.setStrVal("GO HOME");
        b.setStrVal("GO HOME");
        Assert.assertEquals("I,PC,GPN,PC,PC,PC,GPN,PC,PC,PC,PC,PC,PC,PC,", mcl.getBuffer().toString());
    }

    @Test
    public void testBBean() {
        ISimpleBBean b = new SimpleBBean("HELLO");
        DefaultModelWrapper w = new DefaultModelWrapper();
        ReportingModelChangeListener mcl = new ReportingModelChangeListener();
        b = w.wrapModel(b, createList(mcl));
        Assert.assertEquals("I,", mcl.getBuffer().toString());
        b.setStrVal("WORLD");
        Assert.assertEquals("I,PC,GPN,", mcl.getBuffer().toString());
        b.getStrVal();
        Assert.assertEquals("I,PC,GPN,", mcl.getBuffer().toString());
        b.setStrVal("GO HOME");
        Assert.assertEquals("I,PC,GPN,PC,GPN,", mcl.getBuffer().toString());
    }

    @Test
    public void testBBean01() {
        ISimpleBBean b = new SimpleBBean("HELLO");
        DefaultModelWrapper w = new DefaultModelWrapper();
        ReportingModelChangeListener mcl = new ReportingModelChangeListener();
        b = w.wrapModel(b, createList(mcl));
        Assert.assertEquals("I,", mcl.getBuffer().toString());
        b.setStrVal("WORLD");
        b.setStrVal("WORLD");
        b.setStrVal("WORLD");
        b.setStrVal("WORLD");
        Assert.assertEquals("I,PC,GPN,PC,PC,PC,", mcl.getBuffer().toString());
        b.getStrVal();
        Assert.assertEquals("I,PC,GPN,PC,PC,PC,", mcl.getBuffer().toString());
        b.setStrVal("GO HOME");
        b.setStrVal("GO HOME");
        b.setStrVal("GO HOME");
        b.setStrVal("GO HOME");
        Assert.assertEquals("I,PC,GPN,PC,PC,PC,PC,GPN,PC,PC,PC,", mcl.getBuffer().toString());
    }

    private List<IModelChangeListener> createList(IModelChangeListener... items) {
        ArrayList<IModelChangeListener> re = new ArrayList<IModelChangeListener>();
        for (IModelChangeListener item : items) {
            re.add(item);
        }
        return re;
    }

}
